package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.PermissionMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private PermissionMapper permissionMapper;


	@Override
	public PageResult findAll(QueryPageBean queryPageBean) {
		//
		Integer currentPage = queryPageBean.getCurrentPage();
		Integer pageSize = queryPageBean.getPageSize();
		String queryString = queryPageBean.getQueryString();
		PageHelper.startPage(currentPage,pageSize);
		//
		Page<Permission> page = roleMapper.queryByCondition(queryString);


		return new PageResult(page.getTotal(),page.getResult());
	}

	@Override
	public Role findById(Integer id) {
		//第一步 查询role表
		Role role = roleMapper.findRole(id);
		//第二步 将role所拥有的permission查出来
		Set<Permission> permissions = roleMapper.findPermissions(id);
		//第三部 将role所用户的menu查出来
		Set<Menu> menus =  roleMapper.findMenus(id);

		//
		LinkedHashSet<Menu> menuLinkedHashSet = new LinkedHashSet<>();

		for (Menu menu : menus) {
			menuLinkedHashSet.add(menu);
		}
		//封装
		role.setPermissions(permissions);
		role.setMenus(menuLinkedHashSet);
		return role;

	}

	@Override
	public Map<String, Object> findPermAndMenu() {
		//
		List<Permission> permissionList =  permissionMapper.findAll();
		//
		List<Menu> menuList = roleMapper.findAll();

		Map<String,Object> map = new HashMap<>();
		map.put("permissions",permissionList);
		map.put("menus",menuList);


		return map;
	}

	@Override
	public void update(Role role, Integer[] permissionIds, Integer[] menuIds) {
	//1.修改role和permission中间表
		//a.根据roleId删除中间表
		Integer roleId = role.getId();
		roleMapper.deleteRelat4Perm(roleId);
		//b.根据roleId添加中间表
		for (Integer permissionId : permissionIds) {
			Map<String,Integer> map1 = new HashMap<>();
			map1.put("roleId",roleId);
			map1.put("permissionId",permissionId);
			roleMapper.addRelat4Perm(map1);
		}
	//2.修改role和menu中间表
		//a.根据roleId删除中间表
		roleMapper.deleteRelat4Menu(roleId);
		//b.根据roleId添加中间表
		for (Integer menuId : menuIds) {
			Map<String,Integer> map2 = new HashMap<>();
			map2.put("roleId",roleId);
			map2.put("menuId",menuId);
			roleMapper.addRelat4Menu(map2);
		}
	//3.最后修改role表
		roleMapper.update(role);


	}

	@Override
	public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
		//1.先添加role表,拿到添加后自增的id
		roleMapper.add(role);
		Integer roleId = role.getId();
		//2.添加和permission中间表
		for (Integer permissionId : permissionIds) {
			Map<String,Integer> map1 = new HashMap<>();
			map1.put("roleId",roleId);
			map1.put("permissionId",permissionId);
			roleMapper.addRelat4Perm(map1);
		}
		//3.添加与menu中间表
		for (Integer menuId : menuIds) {
			Map<String,Integer> map2 = new HashMap<>();
			map2.put("roleId",roleId);
			map2.put("menuId",menuId);
			roleMapper.addRelat4Menu(map2);
		}


	}

	@Override
	public void delete(Integer id) {
		//1.先删除与permission的关联表
		roleMapper.deleteRelat4Perm(id);
		//2.再删除与menu的关联表
		roleMapper.deleteRelat4Menu(id);
		//3.最后再删除role表
		roleMapper.delete(id);


	}


}
